home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1998 May
/
Macworld (1998-05).dmg
/
Serious Demos
/
Lasso 2.5 Test Drive
/
Lasso 2.5 CGI
/
Java
/
LassoProxy
/
LassoRequest.java
< prev
next >
Wrap
Text File
|
1997-12-12
|
14KB
|
479 lines
/*
LassoRequest.java
Request parameter to LassoProxy. Add your parameters, then invoke the toString() method
to generate a URL-encoded file specification string.
The toString() method invokes validate() and catches any exceptions it throws. For
debugging purposes, you can invoke validate() from your code and catch the exceptions
yourself to see what's going on.
Copyright © 1996 Blue World Communications, Inc. All rights reserved.
*/
import java.util.*;
import java.net.*;
import java.lang.IllegalArgumentException;
public class LassoRequest
{
// Action types
public final static int SEARCH = 0;
public final static int ADD = 1;
public final static int UPDATE = 2;
public final static int DELETE = 3;
public final static int FIELD_INFO = 4; // returns field names, types and value lists for the fields
public final static int FIND_ALL = 5;
public final static int RANDOM = 6;
// Field operator types
public final static int EQUALS = 0; // search operators
public final static int NOT_EQUALS = 1;
public final static int CONTAINS = 2;
public final static int BEGINS_WITH = 3;
public final static int ENDS_WITH = 4;
public final static int GREATER_THAN = 5;
public final static int GREATER_THAN_EQUALS = 6;
public final static int LESS_THAN = 7;
public final static int LESS_THAN_EQUALS = 8; // check validFieldOperator() if defining
// more search operators
public final static int MAX_RECORDS_DEFAULT = 50;
public final static int ALL = -1; // for retrieving all records
// Logical operator types
public final static int AND = 0; // values for fLogicalOperator
public final static int OR = 1;
// Sort orders
public final static int ASCENDING = 0; // values for fSortOrders
public final static int DESCENDING = 1;
public final static int CUSTOM = 2; // check validSortOrder() if defining new sort orders
private int fAction; // required
private String fDatabaseName; // required
private String fLayoutName; // optional (default to layout 0)
private int fSkipRecords; // for searches (optional)
private int fMaxRecords; // for searches (optional)
private int fLogicalOperator; // for searches (optional)
private int fRecordID; // only needed when acting on a single record
private int fTimeout; // in seconds, for searches (optional)
private Vector fSearchFieldNames; // String
private Vector fSearchFieldValues; // String
private Vector fSearchFieldOperators; // Integer
private Vector fReturnFieldNames; // String
private Vector fSortFields; // String
private Vector fSortOrders; // Integer
private String fLasso; // entity to which the request is sent. i.e. lasso.acgi or action.lasso
public LassoRequest(String lasso) // constructor -- set default/initial values
{
fLasso = new String(lasso);
initFields();
}
public LassoRequest() // constructor which sets the fLasso field to the default: action.lasso
{
fLasso = new String("action.lasso");
initFields();
}
protected void initFields()
{
fSearchFieldNames = new Vector();
fSearchFieldValues = new Vector();
fSearchFieldOperators = new Vector();
fSortFields = new Vector();
fSortOrders = new Vector();
fReturnFieldNames = new Vector();
resetAll();
}
public void resetAll()
{
fDatabaseName = null;
fLayoutName = null;
resetSearch();
}
public void resetSearch()
{
fAction = SEARCH;
fRecordID = -1;
fSkipRecords = 0;
fMaxRecords = MAX_RECORDS_DEFAULT;
fLogicalOperator = AND;
fTimeout = 0;
fSearchFieldNames.removeAllElements();
fSearchFieldValues.removeAllElements();
fSearchFieldOperators.removeAllElements();
fReturnFieldNames.removeAllElements();
resetSort();
}
public void resetSort()
{
fSortFields.removeAllElements();
fSortOrders.removeAllElements();
}
public int numSearchFields()
{ return fSearchFieldNames.size(); }
// set the name of the database use for this request
public final void setDatabaseName(String name)
{
fDatabaseName = new String(name);
}
public final String databaseName()
{ return new String(fDatabaseName); }
public final String layoutName()
{ return new String(fLayoutName); }
// set the name of the layout used for this request
public final void setLayoutName(String name)
{
fLayoutName = new String(name);
}
// set the action for this request
public final void setAction(int actionType) throws IllegalArgumentException
{
if (actionType < SEARCH || actionType > RANDOM)
throw new IllegalArgumentException("Specified action was not in the proper range.");
fAction = actionType;
}
// set the maximun number of records which may be retrieved
public final void setMaxRecords(int max)
{ fMaxRecords = max; }
// set the id of the record this request will be used to retreive
public final void setRecordID(int i)
{ fRecordID = i; }
// set the number of records which will be skipped when Lasso
// retreives the search results
// you can retreive records in sets of X by setting this value
// to the value of maxRecords from the previous request
// and submitting this request again
public final void setSkipRecords(int i)
{ fSkipRecords = i; }
// set the global logical operator
public final void setLogicalOperator(int i)
{
if (i == OR)
fLogicalOperator = OR;
else
fLogicalOperator = AND;
}
// returns the global logical operator
public final int logicalOperator()
{ return fLogicalOperator; }
// set the maximum number of seconds Lasso will wait for
// a response from the database
public final void setTimeout(int i)
{ fTimeout = i; }
// returns the timeout value
public final int timeout()
{ return fTimeout; }
// returns the maxumum number of records which will be retrieved
public final int maxRecords()
{ return fMaxRecords; }
// returns the number of records which will be skipped when the request is submitted
public final int skipRecords()
{ return fSkipRecords; }
// returns the ID of the record this request will operate on
// this is only needs when operating on a single record - an ADD, UPDATE or getting the detail on a
// single record
public final int recordID()
{ return fRecordID; }
// returns the action for this request
public final int action()
{ return fAction; }
// add a specific field to be returned.
// if no return fields are specified, all fields are returned. if any return field is specified,
// only the specified fields will be returned
public void addReturnField( String fieldName )
{
fReturnFieldNames.addElement( new String(fieldName) );
}
// add a field and value to be use in a search
// uses default operator BEGINS_WITH
public void addField( String fieldName, String fieldValue )
{
addField( fieldName, fieldValue, BEGINS_WITH );
}
// add a field and value to be used in a search along with the operator
public synchronized void addField( String fieldName, String fieldValue, int fieldOperator )
{
if ( ( fieldName != null ) && ( fieldName.length() > 0 ) && validFieldOperator( fieldOperator ) )
{
Integer fieldOperatorObj = new Integer( fieldOperator );
fSearchFieldNames.addElement( new String( fieldName ) );
if ( ( fieldValue != null ) && ( fieldValue.length() > 0 ) )
{
fSearchFieldValues.addElement( new String( fieldValue ) );
}
else
{
fSearchFieldValues.addElement( new String() );
}
fSearchFieldOperators.addElement( fieldOperatorObj );
}
}
// remove a field from the field lists
// this will remove all values for the field
// returns the number of values which were removed
public synchronized int removeField(String name)
{
boolean done = false;
int removed = 0;
do
{
try
{
Enumeration enum = fSearchFieldNames.elements();
boolean foundOne = false;
for (int itemNum = 0; enum.hasMoreElements() && !foundOne; ++itemNum)
{
String current = (String)enum.nextElement();
if (current.equals(name))
{
foundOne = true;
fSearchFieldNames.removeElementAt(itemNum);
fSearchFieldValues.removeElementAt(itemNum);
fSearchFieldOperators.removeElementAt(itemNum);
++removed;
}
}
if (!foundOne) done = true;
}
catch(NoSuchElementException e)
{ System.err.println(e.toString()); done = true; }
catch(ArrayIndexOutOfBoundsException e)
{ System.err.println(e.toString()); done = true; }
} while (!done);
return removed;
}
// set the logical operator for the fields added after this
public void startOperator(int op)
{
if (op == LassoRequest.OR)
addField("[op_begin]", "or");
else
addField("[op_begin]", "and");
}
// end the last logical operator
public void endOperator()
{ addField("[op_end]", " "); }
public final boolean validFieldOperator( int fieldOperator )
{
return ( ( fieldOperator >= EQUALS ) && ( fieldOperator <= LESS_THAN_EQUALS ) );
}
// add sort criteria along with a sort order
public synchronized void addSortField( String fieldName, int sortOrder )
{
if ( ( fieldName != null ) && ( fieldName.length() > 0 ) && validSortOrder( sortOrder ) )
{
Integer sortOrderObj = new Integer( sortOrder );
fSortFields.addElement( new String( fieldName ) );
fSortOrders.addElement( sortOrderObj );
}
}
public final boolean validSortOrder( int sortOrder )
{
if ( ( sortOrder >= ASCENDING ) && ( sortOrder <= CUSTOM ) )
return true;
return false;
}
// creates the param string which can be submitted to Lasso
public String toString()
{
StringBuffer fileSpecBuffer = new StringBuffer();
try
{
validate();
fileSpecBuffer.append( fLasso + "?[response]=[nohtml]" );
fileSpecBuffer.append( "&[database]=" );
fileSpecBuffer.append( LassoProxy.prepare( fDatabaseName ) );
if ( fLayoutName != null )
{
fileSpecBuffer.append( "&[layout]=" );
fileSpecBuffer.append( LassoProxy.prepare( fLayoutName ) );
}
if ( fAction == SEARCH || fAction == FIND_ALL)
{
if ( fSkipRecords > 0 )
{
fileSpecBuffer.append( "&[skiprecords]=" );
fileSpecBuffer.append( fSkipRecords );
}
if ( fMaxRecords == ALL )
{
fileSpecBuffer.append( "&[maxrecords]=all" );
}
else if ( fMaxRecords > 0 )
{
fileSpecBuffer.append( "&[maxrecords]=" );
fileSpecBuffer.append( fMaxRecords );
}
if ( fTimeout > 0 )
{
fileSpecBuffer.append( "&[timeout]=" );
fileSpecBuffer.append( fTimeout );
}
if ( fLogicalOperator == OR )
fileSpecBuffer.append( "&[logicalop]=or" );
}
}
catch ( Exception e )
{
System.out.println( "Exception: " + e );
return null;
}
try
{
if ( fSearchFieldNames != null )
{
for ( int i = 0; i < fSearchFieldNames.size(); i++ )
{
if ( fAction == SEARCH )
{
fileSpecBuffer.append( "&[op]=" );
switch ( ((Integer)fSearchFieldOperators.elementAt( i )).intValue() )
{
case EQUALS : fileSpecBuffer.append( "eq" ); break;
case NOT_EQUALS : fileSpecBuffer.append( "neq" ); break;
case CONTAINS : fileSpecBuffer.append( "cn" ); break;
case BEGINS_WITH : fileSpecBuffer.append( "bw" ); break;
case ENDS_WITH : fileSpecBuffer.append( "ew" ); break;
case GREATER_THAN : fileSpecBuffer.append( "gt" ); break;
case GREATER_THAN_EQUALS : fileSpecBuffer.append( "gte" ); break;
case LESS_THAN : fileSpecBuffer.append( "lt" ); break;
case LESS_THAN_EQUALS : fileSpecBuffer.append( "lte" ); break;
default : fileSpecBuffer.append( "bw" );
}
}
fileSpecBuffer.append( "&" );
fileSpecBuffer.append( LassoProxy.prepare( (String)fSearchFieldNames.elementAt( i ) ) );
fileSpecBuffer.append( "=" );
fileSpecBuffer.append( LassoProxy.prepare( (String)fSearchFieldValues.elementAt( i ) ) );
}
}
if (fReturnFieldNames != null)
{
for (int i = 0; i < fReturnFieldNames.size(); ++i)
{
fileSpecBuffer.append("&[returnfield]=");
fileSpecBuffer.append(LassoProxy.prepare((String)fReturnFieldNames.elementAt(i)));
}
}
if ( fSortFields != null )
{
for ( int i = 0; i < fSortFields.size(); i++ )
{
fileSpecBuffer.append( "&[sortfield]=" );
fileSpecBuffer.append( LassoProxy.prepare( (String)fSortFields.elementAt( i ) ) );
fileSpecBuffer.append( "&[sortorder]=" );
switch ( ((Integer)fSortOrders.elementAt( i )).intValue() )
{
case ASCENDING : fileSpecBuffer.append( "ascend" ); break;
case DESCENDING : fileSpecBuffer.append( "descend" ); break;
case CUSTOM : fileSpecBuffer.append( "custom" ); break;
default : fileSpecBuffer.append( "ascend" );
}
}
}
}
catch ( Exception e )
{
System.out.println( "Exception: " + e );
return null;
}
if ( fRecordID >= 0 )
{
fileSpecBuffer.append( "&[recid]=" );
fileSpecBuffer.append( fRecordID );
}
switch ( fAction )
{
case ADD : fileSpecBuffer.append( "&[add]" ); break;
case UPDATE : fileSpecBuffer.append( "&[update]" ); break;
case DELETE : fileSpecBuffer.append( "&[delete]" ); break;
case FIELD_INFO : fileSpecBuffer.append( "&[show]" ); break;
case FIND_ALL: fileSpecBuffer.append( "&[findall]" ); break;
case RANDOM: fileSpecBuffer.append("&[random]"); break;
default : fileSpecBuffer.append( "&[search]" ); // default is search
}
return fileSpecBuffer.toString();
}
public void validate() throws DatabaseNameException
{
if ( fDatabaseName == null ) throw ( new DatabaseNameException() );
}
}
class DatabaseNameException extends Exception
{
public DatabaseNameException() {};
public String toString()
{
return ( "LassoRequest: database name not provided." );
}
}